home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0017_GIF File format.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  29KB  |  640 lines

  1. Herbert Zarb <panther!jaguar!hzarb@relay.iunet.it>
  2.  
  3.        This Text File explains the format of Gif Files.
  4.  
  5. ---------------------------------------------------------------------------
  6.     G I F (tm)
  7.        Graphics Interchange Format (tm)
  8.  
  9.         A standard defining a mechanism
  10.        For the storage and transmission
  11.      of raster-based Graphics information
  12.  
  13.           June 15, 1987
  14.  
  15.        (c) CompuServe Incorporated, 1987
  16.        All rights reserved
  17.  
  18.      While this document is copyrighted, the information
  19.    contained Within is made available For use in computer
  20.    software Without royalties, or licensing restrictions.
  21.  
  22.    Gif and 'Graphics Interchange Format' are trademarks of
  23.     CompuServe, Incorporated.
  24.       an H&R Block Company
  25.  
  26.    5000 Arlington Centre Blvd.
  27.       Columbus, Ohio 43220
  28.          (614) 457-8600
  29.              Page 2
  30.  
  31.  
  32.        Graphics Interchange Format (GIF) Specification
  33.  
  34.  
  35.         Table of Contents
  36.  
  37.  INTRODUCTION . . . . . . . . . . . . . . . . . page 3
  38.  GENERAL File FORMAT  . . . . . . . . . . . . . page 3
  39.  Gif SIGNATURE  . . . . . . . . . . . . . . . . page 4
  40.  SCREEN DESCRIPTOR  . . . . . . . . . . . . . . page 4
  41.  GLOBAL COLOR MAP . . . . . . . . . . . . . . . page 5
  42.  IMAGE DESCRIPTOR . . . . . . . . . . . . . . . page 6
  43.  LOCAL COLOR MAP  . . . . . . . . . . . . . . . page 7
  44.  RASTER DATA  . . . . . . . . . . . . . . . . . page 7
  45.  Gif TERMINATOR . . . . . . . . . . . . . . . . page 8
  46.  Gif EXTENSION BLOCKS . . . . . . . . . . . . . page 8
  47.  APPendIX A - GLOSSARY  . . . . . . . . . . . . page 9
  48.  APPendIX B - INTERACTIVE SEQUENCES . . . . . . page 10
  49.  APPendIX C - IMAGE PACKAGING & COMPRESSION . . page 12
  50.  APPendIX D - MULTIPLE IMAGE PROCESSING . . . . page 15
  51.  
  52.  
  53. INTRODUCTION
  54.  
  55.  'GIF' (tm) is CompuServe's standard For defining generalized  color
  56.    raster   images.    This   'Graphics  Interchange  Format'  (tm)  allows
  57.    high-quality, high-resolution Graphics to be displayed on a  Variety  of
  58.    Graphics  hardware  and is intended as an exchange and display mechanism
  59.    For Graphics images.  The image format described  in  this  document  is
  60.    designed  to  support  current  and  future image technology and will in
  61.    addition serve as a basis For future CompuServe Graphics products.
  62.  
  63.  The main focus  of  this  document  is  to  provide  the  technical
  64.    information  necessary  For  a  Programmer to implement Gif encoders and
  65.    decoders.  As such, some assumptions are made as to terminology relavent
  66.    to Graphics and Programming in general.
  67.  
  68.  The first section of this document describes the  Gif  data  format
  69.    and its components and applies to all Gif decoders, either as standalone
  70.    Programs or as part of  a  communications  package.   Appendix  B  is  a
  71.    section  relavent to decoders that are part of a communications software
  72.    package and describes the protocol requirements For entering and Exiting
  73.    Gif mode, and responding to host interrogations.  A glossary in Appendix
  74.    A defines some of the terminology used in  this  document.   Appendix  C
  75.    gives  a  detailed  explanation  of  how  the  Graphics  image itself is
  76.    packaged as a series of data Bytes.
  77.  
  78.  
  79.   Graphics Interchange Format Data Definition
  80.  
  81.  
  82.  GENERAL File FORMAT
  83.  
  84.  +-----------------------+
  85.  | +-------------------+ |
  86.  | |   Gif Signature   | |
  87.  | +-------------------+ |
  88.  | +-------------------+ |
  89.  | | Screen Descriptor | |
  90.  | +-------------------+ |
  91.  | +-------------------+ |
  92.  | | Global Color Map  | |
  93.  | +-------------------+ |
  94.  . . .               . . .
  95.  | +-------------------+ |    ---+
  96.  | |  Image Descriptor | |       |
  97.  | +-------------------+ |       |
  98.  | +-------------------+ |       |
  99.  | |  Local Color Map  | |       |-   Repeated 1 to n times
  100.  | +-------------------+ |       |
  101.  | +-------------------+ |       |
  102.  | |    Raster Data    | |       |
  103.  | +-------------------+ |    ---+
  104.  . . .               . . .
  105.  |-    Gif Terminator   -|
  106.  +-----------------------+
  107.  
  108.  
  109.  Gif SIGNATURE
  110.  
  111.  The following Gif Signature identifies  the  data  following  as  a
  112.    valid Gif image stream.  It consists of the following six Characters:
  113.  
  114.       G I F 8 7 a
  115.  
  116.  The last three Characters '87a' may be viewed as a  version  number
  117.    For  this  particular  Gif  definition  and will be used in general as a
  118.    reference  in  documents  regarding  Gif  that   address   any   version
  119.    dependencies.
  120.  
  121.  SCREEN DESCRIPTOR
  122.  
  123.  The Screen Descriptor describes the overall parameters For all  GIF
  124.    images  following.  It defines the overall dimensions of the image space
  125.    or logical screen required, the existance of color mapping  information,
  126.    background  screen color, and color depth information.  This information
  127.    is stored in a series of 8-bit Bytes as described below.
  128.  
  129.        bits
  130.   7 6 5 4 3 2 1 0  Byte #
  131.  +---------------+
  132.  |               |  1
  133.  +-Screen Width -+      Raster width in pixels (LSB first)
  134.  |               |  2
  135.  +---------------+
  136.  |               |  3
  137.  +-Screen Height-+      Raster height in pixels (LSB first)
  138.  |               |  4
  139.  +-+-----+-+-----+      M = 1, Global color map follows Descriptor
  140.  |M|  cr |0|pixel|  5   cr+1 = # bits of color resolution
  141.  +-+-----+-+-----+      pixel+1 = # bits/pixel in image
  142.  |   background  |  6   background=Color index of screen background
  143.  +---------------+          (color is defined from the Global color
  144.  |0 0 0 0 0 0 0 0|  7        map or default map if none specified)
  145.  +---------------+
  146.  
  147.  
  148.  The logical screen width and height can both  be  larger  than  the
  149.    physical  display.   How  images  larger  than  the physical display are
  150.    handled is Implementation dependent and can take advantage  of  hardware
  151.    Characteristics  (e.g.   Macintosh scrolling Windows).  Otherwise images
  152.    can be clipped to the edges of the display.
  153.  
  154.  The value of 'pixel' also defines  the  maximum  number  of  colors
  155.    Within  an  image.   The  range  of  values  For 'pixel' is 0 to 7 which
  156.    represents 1 to 8 bits.  This translates to a range of 2 (B & W) to  256
  157.    colors.   Bit  3 of Word 5 is reserved For future definition and must be
  158.    zero.
  159.  
  160.  
  161.  GLOBAL COLOR MAP
  162.  
  163.  The Global Color Map is optional but recommended For  images  where
  164.    accurate color rendition is desired.  The existence of this color map is
  165.    indicated in the 'M' field of Byte 5 of the Screen Descriptor.  A  color
  166.    map  can  also  be associated With each image in a Gif File as described
  167.    later.  However this  global  map  will  normally  be  used  because  of
  168.    hardware  restrictions  in equipment available today.  In the individual
  169.    Image Descriptors the 'M' flag will normally be  zero.   if  the  Global
  170.    Color  Map  is  present,  it's definition immediately follows the Screen
  171.    Descriptor.   The  number  of  color  map  entries  following  a  Screen
  172.    Descriptor  is equal to 2**(# bits per pixel), where each entry consists
  173.    of three Byte values representing the relative intensities of red, green
  174.    and blue respectively.  The structure of the Color Map block is:
  175.  
  176.        bits
  177.   7 6 5 4 3 2 1 0  Byte #
  178.  +---------------+
  179.  | red intensity |  1    Red value For color index 0
  180.  +---------------+
  181.  |green intensity|  2    Green value For color index 0
  182.  +---------------+
  183.  | blue intensity|  3    Blue value For color index 0
  184.  +---------------+
  185.  | red intensity |  4    Red value For color index 1
  186.  +---------------+
  187.  |green intensity|  5    Green value For color index 1
  188.  +---------------+
  189.  | blue intensity|  6    Blue value For color index 1
  190.  +---------------+
  191.  :               :       (Continues For remaining colors)
  192.  
  193.  Each image pixel value received will be displayed according to  its
  194.    closest match With an available color of the display based on this color
  195.    map.  The color components represent a fractional intensity  value  from
  196.    none  (0)  to  full (255).  White would be represented as (255,255,255),
  197.    black as (0,0,0) and medium yellow as (180,180,0).  For display, if  the
  198.    device  supports fewer than 8 bits per color component, the higher order
  199.    bits of each component are used.  In the creation of  a  Gif  color  map
  200.    entry  With  hardware  supporting  fewer  than 8 bits per component, the
  201.    component values For the hardware  should  be  converted  to  the  8-bit
  202.    format With the following calculation:
  203.  
  204.  <map_value> = <component_value>*255/(2**<nbits> -1)
  205.  
  206.  This assures accurate translation of colors For all  displays.   In
  207.    the  cases  of  creating  Gif images from hardware Without color palette
  208.    capability, a fixed palette should be created  based  on  the  available
  209.    display  colors For that hardware.  if no Global Color Map is indicated,
  210.    a default color map is generated internally  which  maps  each  possible
  211.    incoming  color  index to the same hardware color index modulo <n> where
  212.    <n> is the number of available hardware colors.
  213.  
  214.  
  215.  IMAGE DESCRIPTOR
  216.  
  217.  The Image Descriptor defines the actual placement  and  extents  of
  218.    the  following  image Within the space defined in the Screen Descriptor.
  219.    Also defined are flags to indicate the presence of a local color  lookup
  220.    map, and to define the pixel display sequence.  Each Image Descriptor is
  221.    introduced by an image separator  Character.   The  role  of  the  Image
  222.    Separator  is simply to provide a synchronization Character to introduce
  223.    an Image Descriptor.  This is desirable if a Gif File happens to contain
  224.    more  than  one  image.   This  Character  is defined as 0x2C hex or ','
  225.    (comma).  When this Character is encountered between images,  the  Image
  226.    Descriptor will follow immediately.
  227.  
  228.  Any Characters encountered between the end of a previous image  and
  229.    the image separator Character are to be ignored.  This allows future GIF
  230.    enhancements to be present in newer image formats and yet ignored safely
  231.    by older software decoders.
  232.  
  233.  
  234.        bits
  235.   7 6 5 4 3 2 1 0  Byte #
  236.  +---------------+
  237.  |0 0 1 0 1 1 0 0|  1    ',' - Image separator Character
  238.  +---------------+
  239.  |               |  2    Start of image in pixels from the
  240.  +-  Image Left -+       left side of the screen (LSB first)
  241.  |               |  3
  242.  +---------------+
  243.  |               |  4
  244.  +-  Image Top  -+       Start of image in pixels from the
  245.  |               |  5    top of the screen (LSB first)
  246.  +---------------+
  247.  |               |  6
  248.  +- Image Width -+       Width of the image in pixels (LSB first)
  249.  |               |  7
  250.  +---------------+
  251.  |               |  8
  252.  +- Image Height-+       Height of the image in pixels (LSB first)
  253.  |               |  9
  254.  +-+-+-+-+-+-----+       M=0 - Use global color map, ignore 'pixel'
  255.  |M|I|0|0|0|pixel| 10    M=1 - Local color map follows, use 'pixel'
  256.  +-+-+-+-+-+-----+       I=0 - Image formatted in Sequential order
  257.     I=1 - Image formatted in Interlaced order
  258.     pixel+1 - # bits per pixel For this image
  259.  
  260.  The specifications For the image position and size must be confined
  261.    to  the  dimensions defined by the Screen Descriptor.  On the other hand
  262.    it is not necessary that the image fill the entire screen defined.
  263.  
  264.  
  265.  LOCAL COLOR MAP
  266.  
  267.  A Local Color Map is optional and defined here For future use.   If
  268.    the  'M' bit of Byte 10 of the Image Descriptor is set, then a color map
  269.    follows the Image Descriptor that applies only to the  following  image.
  270.    At the end of the image, the color map will revert to that defined after
  271.    the Screen Descriptor.  Note that the 'pixel' field of Byte  10  of  the
  272.    Image  Descriptor  is used only if a Local Color Map is indicated.  This
  273.    defines the parameters not only For the image pixel size, but determines
  274.    the  number  of color map entries that follow.  The bits per pixel value
  275.    will also revert to the value specified in the  Screen  Descriptor  when
  276.    processing of the image is complete.
  277.  
  278.  RASTER DATA
  279.  
  280.  The format of the actual image is defined as the  series  of  pixel
  281.    color  index  values that make up the image.  The pixels are stored left
  282.    to right sequentially For an image row.  By default each  image  row  is
  283.    written  sequentially, top to bottom.  In the Case that the Interlace or
  284.    'I' bit is set in Byte 10 of the Image Descriptor then the row order  of
  285.    the  image  display  follows  a  four-pass process in which the image is
  286.    filled in by widely spaced rows.  The first pass Writes every  8th  row,
  287.    starting  With  the top row of the image Window.  The second pass Writes
  288.    every 8th row starting at the fifth row from the top.   The  third  pass
  289.    Writes every 4th row starting at the third row from the top.  The fourth
  290.    pass completes the image, writing  every  other  row,  starting  at  the
  291.    second row from the top.  A Graphic description of this process follows:
  292.  
  293.  
  294.    Image
  295.    Row  Pass 1  Pass 2  Pass 3  Pass 4          Result
  296.    ---------------------------------------------------
  297.      0  **1a**                                  **1a**
  298.      1                          **4a**          **4a**
  299.      2                  **3a**                  **3a**
  300.      3                          **4b**          **4b**
  301.      4          **2a**                          **2a**
  302.      5                          **4c**          **4c**
  303.      6                  **3b**                  **3b**
  304.      7                          **4d**          **4d**
  305.      8  **1b**                                  **1b**
  306.      9                          **4e**          **4e**
  307.     10                  **3c**                  **3c**
  308.     11                          **4f**          **4f**
  309.     12          **2b**                          **2b**
  310.    . . .
  311.  
  312.  
  313.  
  314.  The image pixel values are processed as a series of  color  indices
  315.    which  map  into the existing color map.  The resulting color value from
  316.    the map is what is actually displayed.  This series  of  pixel  indices,
  317.    the  number  of  which  is equal to image-width*image-height pixels, are
  318.    passed to the Gif image data stream one value per pixel, compressed  and
  319.    packaged  according  to  a  version  of the LZW compression algorithm as
  320.    defined in Appendix C.
  321.  
  322.  Gif TERMINATOR
  323.  
  324.  In order to provide a synchronization For the termination of a  GIF
  325.    image  File,  a  Gif  decoder  will process the end of Gif mode when the
  326.    Character 0x3B hex or ';' is found after an image  has  been  processed.
  327.    By  convention  the  decoding software will pause and wait For an action
  328.    indicating that the user is ready to continue.  This may be  a  carriage
  329.    return  entered  at  the  keyboard  or  a  mouse click.  For interactive
  330.    applications this user action must  be  passed  on  to  the  host  as  a
  331.    carriage  return  Character  so  that the host application can continue.
  332.    The decoding software will then typically leave Graphics mode and resume
  333.    any previous process.
  334.  
  335.  
  336.  Gif EXTENSION BLOCKS
  337.  
  338.  To provide For orderly extension of the Gif definition, a mechanism
  339.    For  defining  the  packaging  of extensions Within a Gif data stream is
  340.    necessary.  Specific Gif extensions are to be defined and documented  by
  341.    CompuServe in order to provide a controlled enhancement path.
  342.  
  343.  Gif Extension Blocks are packaged in a manner similar to that  used
  344.    by the raster data though not compressed.  The basic structure is:
  345.  
  346.   7 6 5 4 3 2 1 0  Byte #
  347.  +---------------+
  348.  |0 0 1 0 0 0 0 1|  1       '!' - Gif Extension Block Introducer
  349.  +---------------+
  350.  | Function code |  2       Extension Function code (0 to 255)
  351.  +---------------+    ---+
  352.  |  Byte count   |       |
  353.  +---------------+       |
  354.  :               :       +-- Repeated as many times as necessary
  355.  |func data Bytes|       |
  356.  :               :       |
  357.  +---------------+    ---+
  358.  . . .       . . .
  359.  +---------------+
  360.  |0 0 0 0 0 0 0 0|       zero Byte count (terminates block)
  361.  +---------------+
  362.  
  363.  A Gif Extension Block may immediately preceed any Image  Descriptor
  364.    or occur before the Gif Terminator.
  365.  
  366.  All Gif decoders must be able to recognize  the  existence  of  GIF
  367.    Extension  Blocks  and  read past them if unable to process the Function
  368.    code.  This ensures that older decoders will be able to process extended
  369.    Gif   image   Files   in  the  future,  though  Without  the  additional
  370.    Functionality.
  371.  
  372.  
  373.      GLOSSARY
  374.  
  375. Pixel - The smallest picture element of a  Graphics  image.   This  usually
  376.    corresponds  to  a single dot on a Graphics screen.  Image resolution is
  377.    typically given in Units of  pixels.   For  example  a  fairly  standard
  378.    Graphics  screen  format  is  one 320 pixels across and 200 pixels high.
  379.    Each pixel can  appear  as  one  of  several  colors  depending  on  the
  380.    capabilities of the Graphics hardware.
  381.  
  382. Raster - A horizontal row of pixels representing one line of an  image.   A
  383.    typical method of working With images since most hardware is oriented to
  384.    work most efficiently in this manner.
  385.  
  386. LSB - Least Significant Byte.  Refers to a convention For two Byte  numeric
  387.    values in which the less significant Byte of the value preceeds the more
  388.    significant Byte.  This convention is typical on many microcomputers.
  389.  
  390. Color Map - The list of definitions of each color  used  in  a  Gif  image.
  391.    These  desired  colors are converted to available colors through a table
  392.    which is derived by assigning an incoming color index (from  the  image)
  393.    to  an  output  color  index  (of  the  hardware).   While the color map
  394.    definitons are specified in a Gif image, the output  pixel  colors  will
  395.    Vary  based  on  the  hardware used and its ability to match the defined
  396.    color.
  397.  
  398. Interlace - The method of displaying a Gif image in which  multiple  passes
  399.    are  made,  outputting  raster  lines  spaced  apart to provide a way of
  400.    visualizing the general content of an entire image  before  all  of  the
  401.    data has been processed.
  402.  
  403. B Protocol - A CompuServe-developed error-correcting File transfer protocol
  404.    available  in  the  public  domain  and implemented in CompuServe VIDTEX
  405.    products.  This error checking mechanism will be used  in  transfers  of
  406.    Gif images For interactive applications.
  407.  
  408. LZW - A sophisticated data compression algorithm  based  on  work  done  by
  409.    Lempel-Ziv  &  Welch  which  has  the feature of very efficient one-pass
  410.    encoding and decoding.  This allows the image  to  be  decompressed  and
  411.    displayed  at  the  same  time.   The  original  article from which this
  412.    technique was adapted is:
  413.  
  414.    Terry  A.   Welch,  "A  Technique  For  High   Performance   Data
  415.    Compression", IEEE Computer, vol 17 no 6 (June 1984)
  416.  
  417.  This basic algorithm is also used in the  public  domain  ARC  File
  418.    compression  utilities.   The  CompuServe  adaptation  of LZW For Gif is
  419.    described in Appendix C.
  420.  
  421.     Gif Sequence Exchanges For an Interactive Environment
  422.  
  423.  
  424.  The following sequences are defined For use  in  mediating  control
  425.    between a Gif sender and Gif receiver over an interactive communications
  426.    line.  These  sequences  do  not  apply  to  applications  that  involve
  427.    downloading  of  static  Gif  Files and are not considered part of a GIF
  428.    File.
  429.  
  430.  Gif CAPABILITIES ENQUIRY
  431.  
  432.  The GCE sequence is issued from a host and requests an  interactive
  433.    Gif  decoder  to  return  a  response  message that defines the Graphics
  434.    parameters For the decoder.  This involves returning  information  about
  435.    available screen sizes, number of bits/color supported and the amount of
  436.    color detail supported.  The escape sequence For the GCE is defined as:
  437.  
  438.  ESC [ > 0 g     (g is lower case, spaces inserted For clarity)
  439.     (0x1B 0x5B 0x3E 0x30 0x67)
  440.  
  441.  
  442.  Gif CAPABILITIES RESPONSE
  443.  
  444.  The Gif Capabilities Response message is returned by an interactive
  445.    Gif  decoder  and  defines  the  decoder's  display capabilities For all
  446.    Graphics modes that are supported by the software.  Note that  this  can
  447.    also include Graphics Printers as well as a monitor screen.  The general
  448.    format of this message is:
  449.  
  450.  
  451.      #version;protocol{;dev, width, height, color-bits, color-res}... <CR>
  452.  
  453.    '#'          - GCR identifier Character (Number Sign)
  454.    version      - Gif format version number;  initially '87a'
  455.    protocol='0' - No end-to-end protocol supported by decoder
  456.     Transfer as direct 8-bit data stream.
  457.    protocol='1' - Can use an error correction protocol to transfer Gif data
  458.         interactively from the host directly to the display.
  459.  
  460.    dev = '0'    - Screen parameter set follows
  461.    dev = '1'    - Printer parameter set follows
  462.  
  463.    width        - Maximum supported display width in pixels
  464.    height       - Maximum supported display height in pixels
  465.    color-bits   - Number of  bits  per  pixel  supported.   The  number  of
  466.         supported colors is therefore 2**color-bits.
  467.    color-res    - Number of bits  per  color  component  supported  in  the
  468.         hardware  color  palette.   if  color-res  is  '0'  then  no
  469.         hardware palette table is available.
  470.  
  471.  
  472.  Note that all values in the  GCR  are  returned  as  ASCII  decimal
  473.    numbers and the message is terminated by a Carriage Return Character.
  474.  
  475.  The  following   GCR   message   describes   three   standard   EGA
  476.    configurations  With  no  Printer;  the Gif data stream can be processed
  477.    Within an error correcting protocol:
  478.  
  479.  #87a;1 ;0,320,200,4,0 ;0,640,200,2,2 ;0,640,350,4,2<CR>
  480.  
  481.  
  482.  ENTER Gif GraphICS MODE
  483.  
  484.  Two sequences are currently defined to invoke  an  interactive  GIF
  485.    decoder into action.  The only difference between them is that different
  486.    output media are selected.  These sequences are:
  487.  
  488.      ESC [ > 1 g   Display Gif image on screen
  489.      (0x1B 0x5B 0x3E 0x31 0x67)
  490.  
  491.      ESC [ > 2 g   Display image directly to an attached Graphics  Printer.
  492.      The  image  may optionally be displayed on the screen as
  493.      well.
  494.      (0x1B 0x5B 0x3E 0x32 0x67)
  495.  
  496.  Note that the 'g' Character terminating each sequence is  in  lower
  497.    case.
  498.  
  499.  
  500.  INTERACTIVE ENVIRONMENT
  501.  
  502.  The assumed environment For the transmission of Gif image data from
  503.    an  interactive  application  is  a  full 8-bit data stream from host to
  504.    micro.  All 256 Character codes must be transferrable.  The establishing
  505.    of  an 8-bit data path For communications will normally be taken care of
  506.    by the host application Programs.  It is however  up  to  the  receiving
  507.    communications Programs supporting Gif to be able to receive and pass on
  508.    all 256 8-bit codes to the Gif decoder software.
  509.  
  510.  The Raster Data stream that represents the actual output image  can
  511.    be represented as:
  512.  
  513.   7 6 5 4 3 2 1 0
  514.  +---------------+
  515.  |   code size   |
  516.  +---------------+     ---+
  517.  |blok Byte count|        |
  518.  +---------------+        |
  519.  :               :        +-- Repeated as many times as necessary
  520.  |  data Bytes   |        |
  521.  :               :        |
  522.  +---------------+     ---+
  523.  . . .       . . .
  524.  +---------------+
  525.  |0 0 0 0 0 0 0 0|       zero Byte count (terminates data stream)
  526.  +---------------+
  527.  
  528.  The conversion of the image from a series  of  pixel  values  to  a
  529.    transmitted or stored Character stream involves several steps.  In brief
  530.    these steps are:
  531.  
  532.    1.  Establish the Code Size -  Define  the  number  of  bits  needed  to
  533.        represent the actual data.
  534.  
  535.    2.  Compress the Data - Compress the series of image pixels to a  series
  536.        of compression codes.
  537.  
  538.    3.  Build a Series of Bytes - Take the  set  of  compression  codes  and
  539.        convert to a String of 8-bit Bytes.
  540.  
  541.    4.  Package the Bytes - Package sets of Bytes into blocks  preceeded  by
  542.        Character counts and output.
  543.  
  544.  
  545.  
  546. ESTABLISH CODE SIZE
  547.  
  548.  The first Byte of the Gif Raster Data stream is a value  indicating
  549.    the minimum number of bits required to represent the set of actual pixel
  550.    values.  Normally this will be the same as the  number  of  color  bits.
  551.    Because  of  some  algorithmic Constraints however, black & white images
  552.    which have one color bit must be indicated as having a code size  of  2.
  553.    This  code size value also implies that the compression codes must start
  554.    out one bit longer.
  555.  
  556.  
  557. COMPRESSION
  558.  
  559.  The LZW algorithm converts a series of data values into a series of
  560.    codes  which may be raw values or a code designating a series of values.
  561.    Using Text Characters as an analogy,  the  output  code  consists  of  a
  562.    Character or a code representing a String of Characters.
  563.  
  564.  The LZW algorithm used in  Gif  matches  algorithmically  With  the
  565.    standard LZW algorithm With the following differences:
  566.  
  567.    1.  A   special   Clear   code   is    defined    which    resets    all
  568.        compression/decompression parameters and tables to a start-up state.
  569.        The value of this code is 2**<code size>.  For example if  the  code
  570.        size  indicated  was 4 (image was 4 bits/pixel) the Clear code value
  571.        would be 16 (10000 binary).  The Clear code can appear at any  point
  572.        in the image data stream and therefore requires the LZW algorithm to
  573.        process succeeding codes as if  a  new  data  stream  was  starting.
  574.        Encoders  should output a Clear code as the first code of each image
  575.        data stream.
  576.  
  577.    2.  An end of Information code is defined that explicitly indicates  the
  578.        end  of  the image data stream.  LZW processing terminates when this
  579.        code is encountered.  It must be the last code output by the encoder
  580.        For an image.  The value of this code is <Clear code>+1.
  581.  
  582.    3.  The first available compression code value is <Clear code>+2.
  583.  
  584.    4.  The output codes are of Variable length, starting  at  <code size>+1
  585.        bits  per code, up to 12 bits per code.  This defines a maximum code
  586.        value of 4095 (hex FFF).  Whenever the LZW code value  would  exceed
  587.        the  current  code length, the code length is increased by one.  The
  588.        packing/unpacking of these codes must then be altered to reflect the
  589.        new code length.
  590.  
  591.  
  592. BUILD 8-BIT ByteS
  593.  
  594.  Because the LZW compression  used  For  Gif  creates  a  series  of
  595.    Variable  length  codes, of between 3 and 12 bits each, these codes must
  596.    be reformed into a series of 8-bit Bytes that  will  be  the  Characters
  597.    actually stored or transmitted.  This provides additional compression of
  598.    the image.  The codes are formed into a stream of bits as if  they  were
  599.    packed  right to left and then picked off 8 bits at a time to be output.
  600.    Assuming a Character Array of 8 bits per Character and using 5 bit codes
  601.    to be packed, an example layout would be similar to:
  602.  
  603.   Byte n       Byte 5   Byte 4   Byte 3   Byte 2   Byte 1
  604.  +-.....-----+--------+--------+--------+--------+--------+
  605.  | and so on |hhhhhggg|ggfffffe|eeeedddd|dcccccbb|bbbaaaaa|
  606.  +-.....-----+--------+--------+--------+--------+--------+
  607.  
  608.  Note that the physical  packing  arrangement  will  change  as  the
  609.    number  of  bits per compression code change but the concept remains the
  610.    same.
  611.  
  612. PACKAGE THE ByteS
  613.  
  614.  Once the Bytes have been created, they are grouped into blocks  for
  615.    output by preceeding each block of 0 to 255 Bytes With a Character count
  616.    Byte.  A block With a zero Byte count terminates the Raster Data  stream
  617.    For  a  given  image.  These blocks are what are actually output For the
  618.    Gif image.  This block format has the side effect of allowing a decoding
  619.    Program  the  ability to read past the actual image data if necessary by
  620.    reading block counts and then skipping over the data.
  621. Graphics Interchange Format (GIF)                                   Page 15
  622. Appendix D - Multiple Image Processing
  623.  
  624.  
  625.  Since a  Gif  data  stream  can  contain  multiple  images,  it  is
  626.    necessary  to  describe  processing and display of such a File.  Because
  627.    the image descriptor allows  For  placement  of  the  image  Within  the
  628.    logical  screen,  it is possible to define a sequence of images that may
  629.    each be a partial screen, but in total  fill  the  entire  screen.   The
  630.    guidelines For handling the multiple image situation are:
  631.  
  632.    1.  There is no pause between images.  Each is processed immediately  as
  633.        seen by the decoder.
  634.  
  635.    2.  Each image explicitly overWrites any image  already  on  the  screen
  636.        inside  of  its Window.  The only screen clears are at the beginning
  637.        and end of the  Gif  image  process.   See  discussion  on  the  GIF
  638.        terminator.
  639.  
  640.